# -*- coding=utf-8-*-
"""
@File:memory_selection
@Author:Created by Han X.Y
@Date:on 2021/7/7 15:32 
"""

"""
判断三个数之和，是否等于0
"""
from itertools import combinations
from operator import itemgetter


def sum3(a):
    """
    任意三个元素之和等于0 的结果，如果不存在输出为空
    """
    res = []
    combin3 = combinations(range(len(a)), 3)
    for i in combin3:
        tmp = itemgetter(i[0], i[1], i[2])(a)
        print(sorted(tmp), sum(tmp))
        if sum(tmp) == 0:
            if sorted(tmp) not in res:
                res.append(sorted(tmp))
    return res


"""
给定一个数组，要求在这个数组中找出3个之和离target最近

"""


def sum3_closet(a, target):
    """
    首先找到组合，
    """
    combs = combinations(a, 3)
    target_combs = None
    target_diff = target
    for i in combs:
        tmp = itemgetter(i[0], i[1], i[2])(a)
        tmp_diff = sum(tmp) - target < target_diff
        if tmp_diff < target_diff:
            target_diff = tmp_diff
            target_combs = tmp
    print(target_diff, target)
    return target_combs


"""
给定一个仅包含2-9的字符串，返回所有他能表示的字母集合

"""


def get_letter_comns(a):
    """
    根据手机九键返回对应的字母组合
    """
    if a == '':
        return []
    letter_map = {i: j for i, j in zip(range(10), [' ', '', 'abc', 'def', 'gki',
                                                   'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'])}

    letter_list = list(" ".join([letter_map[eval(i)] for i in a]))
    print(list(combinations(letter_list, len(a))))
    combs = combinations(letter_list, len(a))
    combs_str = ["".join(i) for i in combs]
    return [i for i in combs_str if len(i.strip()) == len(a)]